'use strict';
const baseUrl = 'https://cxt.diandian.work'
function getImageUrl(url) {
    if (url) {
      if (url.includes('http')) {
        return url
      }
      return baseUrl + url
    }
    return url
}
function setImageUrl(url) {
   if (!url) {
     return url
   }
   return url.replace(baseUrl, '')
}
module.exports = app => {
  const { STRING, INTEGER, DECIMAL, TEXT, DATE, ENUM, BOOLEAN } = app.Sequelize;

  const IdleItem = app.model.define('idle_item', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    code: { type: STRING(50), comment: '编码' },
    name: { type: STRING(100), allowNull: false, comment: '物资/设备名称' },
    category_id: { type: INTEGER, allowNull: false, comment: '分类ID' },
    category_name: { type: STRING},
    type: { type: STRING, allowNull: true, comment: '类型：物资(物资)、设备(设备)' },
    specification: { type: STRING(200), comment: '规格型号' },
    unit: { type: STRING(20), comment: '计量单位' },
    quantity: { type: INTEGER, defaultValue: 0, comment: '数量' },
    condition: { type: STRING, comment: '技术状况，1-10，10为全新' },
    // type: ENUM('available', 'reserved', 'transferred', 'rented', 'sold')
    status: { type: STRING, defaultValue: 'available', comment: '使用状态：可用、已预定、已调拨、已出租、已售出' },
    province: { type: STRING(50), comment: '所在省份' },
    city: { type: STRING(50), comment: '所在城市' },
    address: { type: STRING(200), comment: '详细地址' },
    project_id: { type: INTEGER, allowNull: false, comment: '归属单位(项目/基地)ID' },
    project_name: { type: STRING(100), comment: '归属单位名称' },
    unit_price: { type: DECIMAL(10, 2), defaultValue: 0, comment: '单位原值' },
    total_price: {
      type: DECIMAL(10, 2),
      defaultValue: 0,
      comment: '总原值',
      get() {
        return this.getDataValue('unit_price') * this.getDataValue('quantity')
      }
    },
    discount_rate: { type: DECIMAL(5, 2), defaultValue: 100, comment: '折扣率，百分比' },
    rental_price: { type: STRING, comment: '租售价格' },
    is_public: { type: INTEGER, defaultValue: 1, comment: '是否公开，对外可见' },
    idle_start_time: { type: DATE, comment: '闲置开始时间' },
    images: {
      type: TEXT, comment: '图片URL，多个以逗号分隔',
      // get() {
      //   let images = this.getDataValue('images')
      //   let imagesList = images ? images.split(',') : []
      //   imagesList = imagesList.map(item => getImageUrl(item))
      //   return imagesList.join(',')
      // },
      // set(images) {
      //   let imagesList = images ? images.split(',') : []
      //   imagesList = imagesList.map(item => setImageUrl(item))
      //   this.setDataValue('images', imagesList.join(','))
      // }
    },
    remark: { type: TEXT, comment: '备注' },
    contact_name: { type: STRING(50), comment: '联系人' },
    contact_phone: { type: STRING(20), comment: '联系电话' },
    view_count: { type: INTEGER, defaultValue: 0, comment: '浏览次数' },
    create_user_id: { type: INTEGER, comment: '创建人ID' },
    create_user_name: { type: STRING(50), comment: '创建人姓名' },
    create_time: { type: DATE, defaultValue: app.Sequelize.fn('NOW') },
    update_time: { type: DATE, defaultValue: app.Sequelize.fn('NOW') },
    post_url: {
      type: STRING,
      // get() {
      //   let url = this.getDataValue('post_url')
      //   return getImageUrl(url)
      // },
      // set(url) {
      //   this.setDataValue('post_url', setImageUrl(url))
      // }
    },
  }, {
    tableName: 'idle_item',
    timestamps: false,
  });

  // 定义关联关系
  IdleItem.associate = function() {
    app.model.IdleItem.belongsTo(app.model.IdleCategory, { foreignKey: 'category_id' });
    app.model.IdleItem.belongsTo(app.model.User, { foreignKey: 'create_user_id', as: 'createUser' });
    app.model.IdleItem.hasMany(app.model.IdleTransfer, { foreignKey: 'item_id' });
    app.model.IdleItem.hasMany(app.model.IdleFavorite, { foreignKey: 'item_id' });
  };

  return IdleItem;
};
